package me.helllp.bootman.common.base;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.common.base.Strings;

import me.helllp.bootman.common.utils.ResultUtil;

/**
 * 所有控制层的基类
 * 
 * @author 虎头
 *
 */
public class BaseController {
	
    public BaseController(){
        
    }
    
	/**
	 * 获取 HttpServletResponse
	 * @return
	 */
    public HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }
    
    /**
     * 获取 HttpServletRequest
     * 
     * @return
     */
    public HttpServletRequest getRequet(){
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;    	
    }
    
    /**
     * 获取所有请求的值
     */
    public Map<String, String> getRequestParameters() {
        Map<String, String> paramsMap = new HashMap<String,String>();
        
        HttpServletRequest request = this.getRequet();
        Enumeration<String> paramEnums = request.getParameterNames();
        
        String paramName = "";
        
        while ( paramEnums.hasMoreElements()){
            paramName = paramEnums.nextElement();
            paramsMap.put(paramName, request.getParameter(paramName));
        }
        
        return paramsMap;
    }
    
    public BaseReturn genCheckResult(BindingResult result){
    	return ResultUtil.genCheckResult(result);
    }
    
    public BaseReturn genSuccesResult(Object o){
    	return ResultUtil.genSuccessResult(o);
    }
}
